package top.melody.ecommerce.webapps.www.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.melody.ecommerce.common.util.JsonMapper;
import top.melody.ecommerce.platform.handler.enums.ResultMsg;
import top.melody.ecommerce.webapps.www.base.Constant;
import top.melody.ecommerce.webapps.www.weblogic.AuthenticationWeblogic;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * @author: blentle
 * @group: rd
 * @createdate: 2016/11/11 9:55
 * @mail: blentle.huan.ren@gmail.com
 * @description: 商家(生产者资质认证校验企业名称)
 * @since:1.0
 */
@Controller
@RequestMapping("/producer/auth")
public class AuthenticationValidateController {

    private static final Logger logger = LoggerFactory.getLogger(AuthenticationValidateController.class);
    @Resource
    private AuthenticationWeblogic authenticationWeblogic;
    @Resource
    private ResourceBundleMessageSource i18nSource;

    @RequestMapping("/enterprise/validate")
    @ResponseBody
    public String validateEnterpriseName(String enterpriseName) {
        ResultMsg resultMsg = new ResultMsg(ResultMsg.CODE_E, i18nSource.getMessage(Constant.ERROR_OPERATE_FAILED, null, Locale.CHINA));
        try {
            //todo：校验企业是否认证过，即是否重名
            boolean isExists = authenticationWeblogic.isExistsEnterprise(enterpriseName);
            resultMsg.setResult(isExists);
            resultMsg.setStatusCode(ResultMsg.CODE_S);
        } catch (Exception e) {
            logger.error("occurred exception when load shopping cart number ...", e);
        }
        return JsonMapper.nonEmptyMapper().toJson(resultMsg);
    }

}
